package han.Chensing.CCirnosLibrary.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

public class Base64Util {
    public static String objectToBase64String(Object o) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        SerialUtil.toStream(o,byteArrayOutputStream);
        String string = bytesToBase64String(byteArrayOutputStream.toByteArray());
        byteArrayOutputStream.close();
        return string;
    }

    public static <T>T base64ToObject(String base64,Class<T> tClass) throws IOException, ClassNotFoundException {
        byte[] decode = Base64.getDecoder().decode(base64);
        ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(decode);
        T t = SerialUtil.fromStream(byteArrayInputStream, tClass);
        byteArrayInputStream.close();
        return t;
    }

    public static String bytesToBase64String(byte[] bytes){
        return Base64.getEncoder().encodeToString(bytes);
    }
}
